package xin.codedream.codelog.web.controller.admin;

import cn.hutool.extra.servlet.ServletUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import xin.codedream.codelog.model.entity.Logs;
import xin.codedream.codelog.model.entity.Tag;
import xin.codedream.codelog.model.enums.LogsEnum;
import xin.codedream.codelog.model.enums.PageInfoEnum;
import xin.codedream.codelog.model.enums.ResponseEnum;
import xin.codedream.codelog.model.global.ResponseData;
import xin.codedream.codelog.service.LogsService;
import xin.codedream.codelog.service.TagsService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 标签功能控制层
 *
 * @author NGLSL
 * @date 2018/8/13
 */
@Slf4j
@RestController
@RequestMapping("/admin/tags")
public class TagsController {
    @Resource
    private TagsService tagsService;
    @Resource
    private LogsService logsService;
    @Resource
    private HttpServletRequest request;

    /**
     * 获取所有标签，返回JSON
     *
     * @param pageNum  第N页
     * @param pageSize 显示多少条数据
     * @return ResponseData 响应的数据
     */
    @PostMapping("/showAllTags")
    public ResponseData showAllTagsJson(Integer pageNum, Integer pageSize) {
        ResponseData responseData = new ResponseData(ResponseEnum.SUCCESS_CODE);
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            pageSize = PageInfoEnum.PAGE_DEFAULT_NUM.getValue();
            pageNum = PageInfoEnum.PAGE_DEFAULT_SIZE.getValue();
        }
        List<Tag> allTags = tagsService.findAllTags(pageNum, pageSize);
        Map<String, Object> data = new HashMap<>(1);
        data.put("allTags", allTags);
        responseData.setData(data);
        return responseData;
    }

    /**
     * 获取所有标签，返回JSON
     *
     * @param pageNum  第N页
     * @param pageSize 显示多少条数据
     * @return ModelAndView 返回的页面
     */
    @GetMapping("/showAllTags")
    public ModelAndView showAllTagsModel(Integer pageNum, Integer pageSize) {
        ModelAndView mv = new ModelAndView("/admin/admin_tags");
        if (Objects.isNull(pageNum) || Objects.isNull(pageSize)) {
            pageSize = PageInfoEnum.TAG_DEFAULT_NUM.getValue();
            pageNum = PageInfoEnum.PAGE_DEFAULT_SIZE.getValue();
        }
        List<Tag> tags = tagsService.findAllTags(pageNum, pageSize);
        PageInfo<Tag> pageInfo = new PageInfo<>(tags);
        mv.addObject("tags", tags);
        mv.addObject("pageInfo", pageInfo);
        mv.addObject("pageNum", pageNum);
        mv.addObject("pageSize", pageSize);
        return mv;
    }


    /**
     * 删除标签
     *
     * @param tagId 标签名称
     * @return ResponseData
     */
    @PostMapping("/deleteTag")
    public ResponseData deleteTag(Long tagId) {
        ResponseData responseData = new ResponseData();
        try {
            boolean result = tagsService.removeTag(tagId);
            if (result) {
                responseData.setCodeMessage(ResponseEnum.DELETE_TAG_SUCCESS);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_TAG.getMessage(),
                        "标签Id：[" + tagId + "]" + ResponseEnum.DELETE_TAG_SUCCESS.getMessage(),
                        ServletUtil.getClientIP(request)));
            } else {
                responseData.setCodeMessage(ResponseEnum.DELETE_TAG_ERROR);
                logsService.saveLogInfo(new Logs(LogsEnum.DELETE_TAG.getMessage(),
                        "标签Id：[" + tagId + "]" + ResponseEnum.DELETE_TAG_ERROR.getMessage(),
                        ServletUtil.getClientIP(request)));
            }
        } catch (Exception e) {
            log.error("标签删除失败：", e);
            responseData.setCodeMessage(ResponseEnum.DELETE_TAG_ERROR);
            logsService.saveLogInfo(new Logs(LogsEnum.DELETE_TAG.getMessage(),
                    "标签Id：[" + tagId + "]" + ResponseEnum.DELETE_TAG_ERROR.getMessage(),
                    ServletUtil.getClientIP(request)));
        }
        return responseData;
    }

    /**
     * 标签详细信息
     *
     * @param tagId   标签Id
     * @param tagType 标签类型
     * @return ModelAndView
     */
    @GetMapping("/detail")
    public ModelAndView detail(Long tagId, String tagType) {
        ModelAndView mv = new ModelAndView("/admin/admin_tags_edit");
        mv.addObject("tagType", tagType);
        if (tagId != null) {
            Tag tag = tagsService.findTagInfoByTagId(tagId);
            mv.addObject("tag", tag);
        }
        return mv;
    }

    @PostMapping("/saveTagInfo")
    public ResponseData saveTagInfo(Tag tag) {
        return null;
    }
}
